package com.view.cn.mvprxjava.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Zengxiaoping on 2016/12/1.
 */

public class RadarView extends View {

	private String[] titles = {"a", "b", "c", "d", "e", "f", "g", "h"};  //标题
	private double[] data = {100, 60, 60, 60, 100, 50, 10, 20};          //数值

	private float maxValue = 100;             //数据最大值
	private int count = data.length;          //数据个数
	private float angle = (float) (Math.PI * 2 / count);
	private float radius;                     //网格最大半径

	private int centerX;                      //中心X
	private int centerY;                      //中心Y

	private Paint mainPaint;        //雷达区画笔
	private Paint textPaint;        //文本画笔
	private Paint valuePaint;       //数据区画笔

	public RadarView(Context context) {
		super(context);
	}

	public RadarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mainPaint = new Paint();
		mainPaint.setColor(Color.BLUE);
		mainPaint.setStyle(Paint.Style.STROKE);

		textPaint = new Paint();
		textPaint.setColor(Color.BLACK);
		textPaint.setStyle(Paint.Style.FILL);
		textPaint.setTextSize(20);

		valuePaint = new Paint();
	}


	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		drawPolygon(canvas);
		drawLines(canvas);
		drawText(canvas);
		drawRegion(canvas);
	}

	//绘制正多边形
	private void drawPolygon(Canvas canvas) {
		Path path = new Path();
		float r = radius / (count - 1);         //计算蜘蛛丝之间的距离(多边形的角和内侧的多边形角之间的距离)
		for (int i = 1; i < count; i++) {
			float curR = r * i;                 //当前半径
			path.reset();                       //Clear any lines and curves from the path, making it empty
			for (int j = 0; j < count; j++) {
				if (j == 0) {
					//移动绘制起点位置多边形的一个角上
					path.moveTo(centerX + curR, centerY);
				} else {
					//根本半径只算出蜘蛛网上每个点的坐标(注意cos是角的临边比斜边)
					float x = (float) (centerX + curR * Math.cos(angle * j));
					float y = (float) (centerY + curR * Math.sin(angle * j));
					path.lineTo(x, y);
				}
			}
			path.close();//闭合路径
			canvas.drawPath(path, mainPaint);
		}
	}

	//绘制直线
	private void drawLines(Canvas canvas) {
		Path path = new Path();
		for (int i = 0; i < count; i++) {
			path.reset();
			path.moveTo(centerX, centerY);
			float x = (float) (centerX + radius * Math.cos(angle * i));
			float y = (float) (centerY + radius * Math.sin(angle * i));
			path.lineTo(x, y);
			canvas.drawPath(path, mainPaint);
		}
	}

	//绘制文字
	private void drawText(Canvas canvas) {
		Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
		float fontHeight = fontMetrics.descent - fontMetrics.ascent;
		for (int i = 0; i < count; i++) {
			float x = (float) (centerX + (radius + fontHeight / 2) * Math.cos(angle * i));
			float y = (float) (centerY + (radius + fontHeight / 2) * Math.sin(angle * i));
			if (angle * i >= 0 && angle * i <= Math.PI / 2) {                       //第4象限
				canvas.drawText(titles[i], x, y, textPaint);
			} else if (angle * i >= 3 * Math.PI / 2 && angle * i <= Math.PI * 2) {  //第3象限
				canvas.drawText(titles[i], x, y, textPaint);
			} else if (angle * i > Math.PI / 2 && angle * i <= Math.PI) {           //第2象限
				float dis = textPaint.measureText(titles[i]);               //文本长度
				canvas.drawText(titles[i], x - dis, y, textPaint);
			} else if (angle * i >= Math.PI && angle * i < 3 * Math.PI / 2) {       //第1象限
				float dis = textPaint.measureText(titles[i]);               //文本长度
				canvas.drawText(titles[i], x - dis, y, textPaint);
			}
		}
	}

	//绘制覆盖区域
	private void drawRegion(Canvas canvas) {
		Path path = new Path();
		valuePaint.setAlpha(255);
		for (int i = 0; i < count; i++) {
			double percent = (data[i] > maxValue ? maxValue : data[i]) / maxValue;   //计算出百分比
//			double percent = data[i] / maxValue;                                     //计算出百分比
			float x = (float) (centerX + radius * Math.cos(angle * i) * percent);    //计算出数据在线上的位置对应的X坐标
			float y = (float) (centerY + radius * Math.sin(angle * i) * percent);    //计算出数据在线上的位置对应的Y坐标
			if (i == 0) {
				path.moveTo(x, centerY);
			} else {
				path.lineTo(x, y);
			}
			//绘制小圆点
			canvas.drawCircle(x, y, 10, valuePaint);
		}
		path.close();

		//绘制填充区域
		valuePaint.setAlpha(127);   //设置透明度
		valuePaint.setStyle(Paint.Style.FILL_AND_STROKE);
		canvas.drawPath(path, valuePaint);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		//得到最大半径
		radius = (float) ((Math.min(w, h) / 2) * 0.9);
		centerX = w / 2;
		centerY = h / 2;
		postInvalidate();
		super.onSizeChanged(w, h, oldw, oldh);
	}


	//设置标题
	public void setTitkls(String[] titles) {
		this.titles = titles;
		invalidate();
	}

	//设置数值
	public void setData(double[] data) {
		this.data = data;
		invalidate();
	}

	//设置蜘蛛网颜色
	public void setMainPaintColor(int color) {
		mainPaint.setColor(color);
		invalidate();
	}

	//设置标题颜色
	public void setTextPaintColor(int color) {
		textPaint.setColor(color);
		invalidate();
	}

	//设置覆盖区域颜色
	public void setValuePainColor(int color) {
		valuePaint.setColor(color);
		invalidate();
	}
}
